#include "drv_usart.h"
#include <stdio.h>
#include <string.h>
//定义字符串命令缓存区（USART2专用）
#define CMD_BUF_LEN 32
char usart2_cmd_buf[CMD_BUF_LEN] = {0};
uint8_t volatile usart2_cmd_len = 0;

//清空命令缓存区
void clear_cmd_buf(void)
{
	memset(usart2_cmd_buf,0,CMD_BUF_LEN);
	usart2_cmd_len = 0;
}
void drv_usart6_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);//开GPIOG时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//开USART6时钟
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_14;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//使能上拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOG, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOG, GPIO_PinSource9, GPIO_AF_USART6);//将PG9复用为USART6
	GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6);//将PG14复用为USART6
	
	USART_InitStruct.USART_BaudRate = 115200;//波特率
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不使用串口的硬件流控模式
	USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//能接收能发送
	USART_InitStruct.USART_Parity = USART_Parity_No;//无校验位
	USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8位数据位
	USART_Init(USART6, &USART_InitStruct);
	USART_ClearFlag(USART6, USART_FLAG_TC);//清除发送完成标志
	USART_Cmd(USART6, ENABLE);

	USART_ITConfig(USART6, USART_IT_RXNE,ENABLE);//串口的“接收数据寄存器非空”中断
	NVIC_InitStruct.NVIC_IRQChannel = USART6_IRQn;//中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能中断通道
	NVIC_Init(&NVIC_InitStruct);
	
}
//语言模块串口
void drv_usart2_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);//开GPIOG时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//开USART6时钟
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//使能上拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);//将PG9复用为USART6
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);//将PG14复用为USART6
	
	USART_InitStruct.USART_BaudRate = 115200;//波特率
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不使用串口的硬件流控模式
	USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//能接收能发送
	USART_InitStruct.USART_Parity = USART_Parity_No;//无校验位
	USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8位数据位
	USART_Init(USART2, &USART_InitStruct);
	USART_ClearFlag(USART2, USART_FLAG_TC);//清除发送完成标志
	USART_Cmd(USART2, ENABLE);

	USART_ITConfig(USART2, USART_IT_RXNE,ENABLE);//串口的“接收数据寄存器非空”中断
	NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;//中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能中断通道
	NVIC_Init(&NVIC_InitStruct);
}
//WIFI模块串口
void drv_usart3_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);//开GPIOG时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//开USART6时钟
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//使能上拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);//将PG9复用为USART6
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);//将PG14复用为USART6
	
	USART_InitStruct.USART_BaudRate = 115200;//波特率
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不使用串口的硬件流控模式
	USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//能接收能发送
	USART_InitStruct.USART_Parity = USART_Parity_No;//无校验位
	USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8位数据位
	USART_Init(USART3, &USART_InitStruct);
	USART_ClearFlag(USART3, USART_FLAG_TC);//清除发送完成标志
	USART_Cmd(USART3, ENABLE);

	USART_ITConfig(USART3, USART_IT_RXNE,ENABLE);//串口的“接收数据寄存器非空”中断
	NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;//中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能中断通道
	NVIC_Init(&NVIC_InitStruct);
	
}
int fputc(int ch,FILE *f)
{
	USART_SendData(USART6, ch);
	while(USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET);
	USART_ClearFlag(USART6, USART_FLAG_TC);
	return ch;
}

